In [ ]:
%%HTML
<script src="require.js"></script>

AI&ML PROJECT - Summer 2023¶

Students :

-Ahmad Khalil Fratekh - 0206882 
-Noor Mohammad Albaw - 0201672 

Project Overview :¶

The project involves working on a dataset of medical students, working on the dataset we went through multiple operations, which include :

1-Data preparation
2-Classification using sklearn
3-Classification using neural networks from keras

and we have set our target to be the "Diabetes" column

In [ ]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.impute import SimpleImputer
from scipy import stats
import os
import seaborn as sns
from sklearn.preprocessing import *
from sklearn.model_selection import *
from sklearn.model_selection import cross_val_score
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
import joblib
from sklearn.metrics import *
from sklearn.utils import shuffle
from sklearn import preprocessing
from pandas.plotting import scatter_matrix
import tensorflow as tf
import keras
from keras import layers
import shutil
from tensorflow.keras import initializers
import plotly.express as px
import plotly.io as pio
pio.renderers.default='notebook'

PART 1¶

Data Preparation :

In [ ]:
data = pd.read_csv('medical_students_dataset.csv')
data.shape
Out[ ]:
(200000, 13)
In [ ]:
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 200000 entries, 0 to 199999
Data columns (total 13 columns):
 #   Column          Non-Null Count   Dtype  
---  ------          --------------   -----  
 0   Student ID      180000 non-null  float64
 1   Age             180000 non-null  float64
 2   Gender          180000 non-null  object 
 3   Height          180000 non-null  float64
 4   Weight          180000 non-null  float64
 5   Blood Type      180000 non-null  object 
 6   BMI             180000 non-null  float64
 7   Temperature     180000 non-null  float64
 8   Heart Rate      180000 non-null  float64
 9   Blood Pressure  180000 non-null  float64
 10  Cholesterol     180000 non-null  float64
 11  Diabetes        180000 non-null  object 
 12  Smoking         180000 non-null  object 
dtypes: float64(9), object(4)
memory usage: 19.8+ MB
In [ ]:
data.isnull().sum()
Out[ ]:
Student ID        20000
Age               20000
Gender            20000
Height            20000
Weight            20000
Blood Type        20000
BMI               20000
Temperature       20000
Heart Rate        20000
Blood Pressure    20000
Cholesterol       20000
Diabetes          20000
Smoking           20000
dtype: int64

Since, as we can see from the plot below, the data is not balanced, we started off by extracting a balanced data to work on¶

In [ ]:
plt.gca().set_title("Diabetes")
sns.countplot(y="Diabetes" , palette='Set3', data = data)
Out[ ]:
<Axes: title={'center': 'Diabetes'}, xlabel='count', ylabel='Diabetes'>
No description has been provided for this image

The following three cells of code, implement and visualize the procedure of extracting our balanced dataset :¶

In [ ]:
data_yes = data[data["Diabetes"]=="Yes"]
data_no = data[data["Diabetes"]=="No"]

pd.DataFrame(data_yes)
pd.DataFrame(data_no)

Med_Stu=pd.concat([data_yes[:18000],data_no[:18000]], ignore_index=True)
Med_Stu["Diabetes"]
Out[ ]:
0        Yes
1        Yes
2        Yes
3        Yes
4        Yes
        ... 
35995     No
35996     No
35997     No
35998     No
35999     No
Name: Diabetes, Length: 36000, dtype: object
In [ ]:
print(Med_Stu.shape)

Med_Stu.info()
(36000, 13)
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 36000 entries, 0 to 35999
Data columns (total 13 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----  
 0   Student ID      32406 non-null  float64
 1   Age             32444 non-null  float64
 2   Gender          32354 non-null  object 
 3   Height          32393 non-null  float64
 4   Weight          32346 non-null  float64
 5   Blood Type      32431 non-null  object 
 6   BMI             32382 non-null  float64
 7   Temperature     32414 non-null  float64
 8   Heart Rate      32447 non-null  float64
 9   Blood Pressure  32419 non-null  float64
 10  Cholesterol     32404 non-null  float64
 11  Diabetes        36000 non-null  object 
 12  Smoking         32311 non-null  object 
dtypes: float64(9), object(4)
memory usage: 3.6+ MB
In [ ]:
plt.gca().set_title("Diabetes")
sns.countplot(y="Diabetes" , palette=['lightsteelblue','slategrey'], data = Med_Stu)
Out[ ]:
<Axes: title={'center': 'Diabetes'}, xlabel='count', ylabel='Diabetes'>
No description has been provided for this image

Now that we have extracted our balanced data, it's time to get rid of unnecessary or full of null values column(s), which (is/are) : "Student ID"¶

In [ ]:
Med_Stu_0= Med_Stu.drop("Student ID", axis=1,inplace=True)
In [ ]:
Med_Stu.hist(bins=50, figsize=(20,15), color='dimgray', ec='lavender')
plt.show()

Med_Stu.describe()
No description has been provided for this image
Out[ ]:
Age Height Weight BMI Temperature Heart Rate Blood Pressure Cholesterol
count 32444.000000 32393.000000 32346.000000 32382.000000 32414.000000 32447.000000 32419.000000 32404.000000
mean 26.043983 174.730780 70.088455 23.392699 98.596852 79.516627 114.530337 184.759844
std 4.894372 14.493838 17.377104 7.093494 0.502917 11.546663 14.398554 37.517835
min 18.000000 150.000996 40.000578 10.081431 96.397835 60.000000 90.000000 120.000000
25% 22.000000 162.149046 55.009155 17.865348 98.260639 70.000000 102.000000 152.000000
50% 26.000000 174.673148 70.082653 22.733757 98.595614 80.000000 114.000000 185.000000
75% 30.000000 187.302386 85.194960 28.084662 98.939748 90.000000 127.000000 218.000000
max 34.000000 199.997940 99.997668 44.314074 100.566498 99.000000 139.000000 249.000000

In the cell below, we can notice that, there's a couple of missing values in all of the features except for the label which raises the need of using an imputer

In [ ]:
Med_Stu.isnull().sum()
Out[ ]:
Age               3556
Gender            3646
Height            3607
Weight            3654
Blood Type        3569
BMI               3618
Temperature       3586
Heart Rate        3553
Blood Pressure    3581
Cholesterol       3596
Diabetes             0
Smoking           3689
dtype: int64

Before getting to the pipeline, we have to seperate the label from other features, afterwards we have to seperate numercial features from the categorical ones, which is implied in the folloing two cells of code¶

In [ ]:
Med_Stu_1= Med_Stu.drop("Diabetes", axis=1)
Med_Stu_label = Med_Stu["Diabetes"].copy()
Med_Stu_1.head(10)
Out[ ]:
Age Gender Height Weight Blood Type BMI Temperature Heart Rate Blood Pressure Cholesterol Smoking
0 32.0 Female 182.537664 55.741083 A 16.729017 98.260293 76.0 130.0 216.0 No
1 34.0 Male NaN 60.882228 B 22.544095 98.963569 89.0 130.0 243.0 NaN
2 33.0 Male 184.718988 93.666944 NaN 27.451322 98.418213 68.0 133.0 180.0 Yes
3 33.0 Male 177.165911 68.129149 O 21.705642 98.201649 NaN 116.0 143.0 Yes
4 33.0 Male 160.463706 55.755226 A 21.653691 99.161461 77.0 NaN 152.0 Yes
5 34.0 Male 174.207898 NaN B 22.292974 98.919826 74.0 129.0 139.0 Yes
6 24.0 Female 162.044348 80.638530 AB 30.709647 98.996462 98.0 137.0 205.0 Yes
7 21.0 Male 171.146689 69.056734 AB 23.575941 98.316070 NaN 93.0 134.0 No
8 34.0 Female 150.942632 90.580214 O 39.756624 97.563234 79.0 135.0 198.0 No
9 21.0 Male 159.633475 89.877838 AB 35.269937 98.592047 71.0 115.0 137.0 No
In [ ]:
Med_Stu_Cat = Med_Stu_1.select_dtypes(include="object").columns
Med_Stu_Num = Med_Stu_1.select_dtypes(exclude="object").columns

The pipeline:¶

After separating the features from the response: -The cell below shows 2 pipelines that are created, after dividing the dataset into two categories, the first one contains the numeric features, where the second one contains the categorical features.

-The first pipeline, (i.e. "NUM_PIPELINE"), is used to, first, fill the missing values of the numerical features, and then scale the numeric features, to make them easier to compare and to operate on them.

-The second one , (i.e. "CAT_PIPELINE"), is used, first, fill the missing values of the categorical features, and then encode the categorical features using the one-hot encoder, so that we can find the correlation between the label (i.e. "Heart_Disease") and the other non-numeric features in the dataset.

-The last pipeline (i.e. "full_pipeline"), basically, combines the first two together.
In [ ]:
NUM_PIPLINE= Pipeline([('num_imp', SimpleImputer(strategy ="median")), ('std_scaler', StandardScaler())])

CAT_PIPLINE= Pipeline([('cat_imp',SimpleImputer(strategy='most_frequent')), ('1_hot_encoder',OneHotEncoder(sparse=False))])
                        
                       
                       
full_pipeline = ColumnTransformer([
        ("num",NUM_PIPLINE, Med_Stu_Num),
        ("cat", CAT_PIPLINE,Med_Stu_Cat),
    ],remainder='passthrough')

med_stu = full_pipeline.fit_transform(Med_Stu_1)

Med_Stu_Prepared=pd.DataFrame(med_stu,columns=full_pipeline.get_feature_names_out())
print(Med_Stu_Prepared.shape)
Med_Stu_Prepared.head()
(36000, 16)
c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\preprocessing\_encoders.py:972: FutureWarning:

`sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value.

Out[ ]:
num__Age num__Height num__Weight num__BMI num__Temperature num__Heart Rate num__Blood Pressure num__Cholesterol cat__Gender_Female cat__Gender_Male cat__Blood Type_A cat__Blood Type_AB cat__Blood Type_B cat__Blood Type_O cat__Smoking_No cat__Smoking_Yes
0 1.282817 0.568260 -0.871013 -0.980244 -0.705013 -0.325128 1.135976 0.877000 1.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0
1 1.713266 -0.003772 -0.558887 -0.116245 0.768728 0.860695 1.135976 1.635549 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0
2 1.498041 0.726921 1.431519 0.612866 -0.374085 -1.054866 1.355525 -0.134399 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0
3 1.498041 0.177541 -0.118916 -0.240822 -0.827904 0.039740 0.111415 -1.173892 0.0 1.0 0.0 0.0 0.0 1.0 0.0 1.0
4 1.498041 -1.037308 -0.870155 -0.248541 1.183418 -0.233911 -0.034951 -0.921043 0.0 1.0 1.0 0.0 0.0 0.0 0.0 1.0
In [ ]:
Med_Stu_Prepared.hist(bins=50, figsize=(20,15), color='dimgray', ec='midnightblue')
plt.show()

Med_Stu_Prepared.describe()
No description has been provided for this image
Out[ ]:
num__Age num__Height num__Weight num__BMI num__Temperature num__Heart Rate num__Blood Pressure num__Cholesterol cat__Gender_Female cat__Gender_Male cat__Blood Type_A cat__Blood Type_AB cat__Blood Type_B cat__Blood Type_O cat__Smoking_No cat__Smoking_Yes
count 3.600000e+04 3.600000e+04 3.600000e+04 3.600000e+04 3.600000e+04 3.600000e+04 3.600000e+04 3.600000e+04 36000.000000 36000.000000 36000.000000 36000.000000 36000.000000 36000.000000 36000.000000 36000.000000
mean 2.084752e-16 3.042505e-16 9.456140e-16 2.456800e-16 -2.901393e-14 -3.720481e-16 3.720481e-16 3.821141e-16 0.444472 0.555528 0.221556 0.222750 0.328278 0.227417 0.818444 0.181556
std 1.000014e+00 1.000014e+00 1.000014e+00 1.000014e+00 1.000014e+00 1.000014e+00 1.000014e+00 1.000014e+00 0.496914 0.496914 0.415299 0.416098 0.469593 0.419170 0.385483 0.385483
min -1.730328e+00 -1.798322e+00 -1.826641e+00 -1.967937e+00 -4.607862e+00 -1.784603e+00 -1.791342e+00 -1.820064e+00 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
25% -8.694298e-01 -8.102296e-01 -8.100104e-01 -7.342704e-01 -6.183490e-01 -7.812143e-01 -7.667805e-01 -8.086649e-01 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000
50% -8.531266e-03 -3.771884e-03 -3.164960e-04 -8.806557e-02 -2.335110e-03 3.974028e-02 -3.495096e-02 6.073076e-03 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000
75% 8.523673e-01 8.092148e-01 8.156271e-01 5.953376e-01 6.297012e-01 7.694777e-01 8.432445e-01 8.208111e-01 1.000000 1.000000 0.000000 0.000000 1.000000 0.000000 1.000000 0.000000
max 1.713266e+00 1.838249e+00 1.815866e+00 3.118318e+00 4.127724e+00 1.772867e+00 1.794623e+00 1.804116e+00 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000

The cell below shows the re-appending of the label to the dataset after it is prepared

In [ ]:
Med_Stu_Prepared["Diabetes"] = Med_Stu_label
Med_Stu_Prepared.shape
Out[ ]:
(36000, 17)

Encoding of the label using the label encoder

In [ ]:
label_encoder = preprocessing.LabelEncoder()
Med_Stu_Prepared["Diabetes"]= label_encoder.fit_transform(Med_Stu_Prepared["Diabetes"])
  
Med_Stu_Prepared=shuffle(Med_Stu_Prepared)
Med_Stu_Prepared.head()
Out[ ]:
num__Age num__Height num__Weight num__BMI num__Temperature num__Heart Rate num__Blood Pressure num__Cholesterol cat__Gender_Female cat__Gender_Male cat__Blood Type_A cat__Blood Type_AB cat__Blood Type_B cat__Blood Type_O cat__Smoking_No cat__Smoking_Yes Diabetes
2311 0.852367 0.402548 -0.214484 -0.422540 1.147982 0.587043 -0.620415 0.427489 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 1
21800 -0.223756 0.645932 -0.000316 -0.052685 2.355241 0.313392 1.794623 -0.724382 0.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 0
15970 -0.008531 1.061236 -0.847606 -1.139046 -0.836823 -0.233911 1.794623 1.157944 1.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 1
18948 -1.730328 -0.606676 -1.044023 -0.627134 -1.353082 0.860695 0.330964 1.466983 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0
22014 0.637143 1.036016 -0.008434 -0.555379 -0.335294 1.590432 -1.205878 1.242227 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 0

The cell below finds the correlation between the label (i.e. "Diabetes"), and the remaining features in the dataset

In [ ]:
corr_matrix=Med_Stu_Prepared.corr(numeric_only=True)
corr_matrix["Diabetes"].sort_values(ascending=False)
Out[ ]:
Diabetes               1.000000
num__Age               0.007611
num__Heart Rate        0.005605
cat__Smoking_No        0.004756
cat__Blood Type_B      0.003668
cat__Blood Type_A      0.003612
num__Weight            0.002761
num__Cholesterol       0.001233
cat__Gender_Female     0.000503
num__BMI               0.000398
cat__Gender_Male      -0.000503
num__Blood Pressure   -0.002450
cat__Blood Type_O     -0.003777
cat__Blood Type_AB    -0.003939
cat__Smoking_Yes      -0.004756
num__Height           -0.005167
num__Temperature      -0.010606
Name: Diabetes, dtype: float64
In [ ]:
attributes = ["num__BMI", "num__Weight","num__Height"]              
fig = px.scatter_matrix(Med_Stu_Prepared,
    dimensions=attributes,
    color="Diabetes",
    title="Scatter matrix of medical students set",
    labels={col:col.replace('num__', ' ') for col in Med_Stu_Prepared.columns})

fig.show()
In [ ]:
ax=sns.set(rc= {"figure.figsize": (8,4)})

ax=sns.boxplot(
    data=Med_Stu_Prepared, x=Med_Stu_Prepared["num__BMI"].round(2), y=Med_Stu_Prepared["num__Weight"].round(0),
    notch=True, showcaps=False,
    flierprops={"marker": "x"},
    boxprops={"facecolor": (.4, .6, .8, .5)},
    medianprops={"color": "coral"},orient="h",dodge=True
)

ax.set(ylabel="Weight")
ax.set(xlabel="BMI")
Out[ ]:
[Text(0.5, 0, 'BMI')]
No description has been provided for this image
In [ ]:
x1=(Med_Stu_Prepared[(Med_Stu_Prepared['Diabetes']==1.0) & (Med_Stu_Prepared['cat__Blood Type_B']==1.0)])
a=x1.shape[0]

x2=(Med_Stu_Prepared[(Med_Stu_Prepared['Diabetes']==1.0) & (Med_Stu_Prepared['cat__Blood Type_A']==1.0)])
b=x2.shape[0]

x3=(Med_Stu_Prepared[(Med_Stu_Prepared['Diabetes']==1.0) & (Med_Stu_Prepared['cat__Blood Type_O']==1.0)])
c=x3.shape[0]

x4=(Med_Stu_Prepared[(Med_Stu_Prepared['Diabetes']==1.0) & (Med_Stu_Prepared['cat__Blood Type_AB']==1.0)])
d=x4.shape[0]



list=[a,b,c,d]
colors=['coral','lightcoral','steelblue','aliceblue']
patterns=('*','\\','+','|')
labels=['B','A','O','AB']

bar=plt.bar(labels,list,label='Blood Type', width=0.35, color=colors)
for i,x in zip(bar,patterns):
    i.set_hatch(x)

plt.ylabel("Diabetes ")
plt.xlabel('Blood Categories')
plt.title('Relation Between blood type and Diabetes ')
plt.show()
No description has been provided for this image

The following three cells of code demonstrate the procedure of splitting the data into a training set and a test set with no label¶

In [ ]:
import numpy as np
def split_train_test (data, test_ratio):
    shuffled_indices = np.random.permutation(len(data))
    test_set_size = int(len(data)*test_ratio)
    test_indices = shuffled_indices[:test_set_size]
    train_indices = shuffled_indices[test_set_size:]
    return data.iloc[train_indices], data.iloc[test_indices]

train_set, test_set = split_train_test(Med_Stu_Prepared, 0.2)
print (len(train_set),"train +", len(test_set), "test")
28800 train + 7200 test
In [ ]:
train = train_set.drop("Diabetes", axis=1) 
l1 = train_set["Diabetes"].copy()
len(l1)
Out[ ]:
28800
In [ ]:
test = test_set.drop("Diabetes", axis=1) 
l2 = test_set["Diabetes"].copy()
test.shape
Out[ ]:
(7200, 16)

PART 2¶

Classifiers used:

1-DecisionTreeClassifier
2-LogisticRegression 
3-SVC
4-RandomForestClassifier
5-KNeighborsClassifier
6-MLPClassifier
7-CalibratedClassifierCV

The model is trained and the accuracy is found on and for each one of the classifiers mentioned above

using the grid search, different parameters are tested for each one of the classifiers

1-DecisionTreeClassifier¶

In [ ]:
tree_clf = DecisionTreeClassifier(random_state=42)
tree_params = [
    {'criterion': ['gini', 'entropy', 'log_loss'],
     'splitter': ['best','random'],
     'max_depth': [3,4,5]}]
grid_search = GridSearchCV(tree_clf,tree_params, cv=3,
                           scoring='accuracy')
grid_search.fit(train, l1)
    
Out[ ]:
GridSearchCV(cv=3, estimator=DecisionTreeClassifier(random_state=42),
             param_grid=[{'criterion': ['gini', 'entropy', 'log_loss'],
                          'max_depth': [3, 4, 5],
                          'splitter': ['best', 'random']}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=DecisionTreeClassifier(random_state=42),
             param_grid=[{'criterion': ['gini', 'entropy', 'log_loss'],
                          'max_depth': [3, 4, 5],
                          'splitter': ['best', 'random']}],
             scoring='accuracy')
DecisionTreeClassifier(random_state=42)
DecisionTreeClassifier(random_state=42)

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for DTC = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'criterion': 'entropy', 'max_depth': 5, 'splitter': 'best'}
Best estimator = DecisionTreeClassifier(criterion='entropy', max_depth=5, random_state=42)
The accuracy for DTC = 0.5084722222222222
precision = 0.5045632333767927
recall = 0.21614074280927115
f1_score = 0.3026392961876832

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

2-LogisticRegression¶

In [ ]:
log_reg = LogisticRegression(max_iter=1000)
log_params = [
    {'C': [1,10,100],
     'fit_intercept': [True,False],
     'n_jobs': [-1,10,16],
    }]
grid_search = GridSearchCV(log_reg,log_params, cv=3,
                           scoring='accuracy')
grid_search.fit(train, l1)
Out[ ]:
GridSearchCV(cv=3, estimator=LogisticRegression(max_iter=1000),
             param_grid=[{'C': [1, 10, 100], 'fit_intercept': [True, False],
                          'n_jobs': [-1, 10, 16]}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=LogisticRegression(max_iter=1000),
             param_grid=[{'C': [1, 10, 100], 'fit_intercept': [True, False],
                          'n_jobs': [-1, 10, 16]}],
             scoring='accuracy')
LogisticRegression(max_iter=1000)
LogisticRegression(max_iter=1000)

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for LogisticRegression = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'C': 1, 'fit_intercept': True, 'n_jobs': -1}
Best estimator = LogisticRegression(C=1, max_iter=1000, n_jobs=-1)
The accuracy for LogisticRegression = 0.5050347222222222
precision = 0.5020544427324088
recall = 0.5459368891371126
f1_score = 0.5230769230769231

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

3-SupportVectorClassifier¶

In [ ]:
svm = SVC()
param_grid = [
    {
     'gamma': [2, 1],
     'C':[0.001,0.1],
    }]

 
grid_search = GridSearchCV(svm, param_grid, cv=3,scoring='accuracy')
grid_search.fit(train[:7000],l1[:7000])
Out[ ]:
GridSearchCV(cv=3, estimator=SVC(),
             param_grid=[{'C': [0.001, 0.1], 'gamma': [2, 1]}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=SVC(),
             param_grid=[{'C': [0.001, 0.1], 'gamma': [2, 1]}],
             scoring='accuracy')
SVC()
SVC()

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for SVC = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'C': 0.001, 'gamma': 2}
Best estimator = SVC(C=0.001, gamma=2)
The accuracy for SVC = 0.5018571633381829
precision = 0.4973611111111111
recall = 1.0
f1_score = 0.6643168537241443

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

4-RandomForestClassifier¶

In [ ]:
RFC= RandomForestClassifier()
param_grid = [{
    'n_estimators':[500,1000],
    'min_samples_split':[2,3],
    'max_depth' :[2,6]
}]
grid_search = GridSearchCV(RFC, param_grid, cv=3, scoring='accuracy',error_score='raise')
grid_search.fit(train[:15000],l1[:15000])
Out[ ]:
GridSearchCV(cv=3, error_score='raise', estimator=RandomForestClassifier(),
             param_grid=[{'max_depth': [2, 6], 'min_samples_split': [2, 3],
                          'n_estimators': [500, 1000]}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, error_score='raise', estimator=RandomForestClassifier(),
             param_grid=[{'max_depth': [2, 6], 'min_samples_split': [2, 3],
                          'n_estimators': [500, 1000]}],
             scoring='accuracy')
RandomForestClassifier()
RandomForestClassifier()

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for RFC = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'max_depth': 6, 'min_samples_split': 2, 'n_estimators': 1000}
Best estimator = RandomForestClassifier(max_depth=6, n_estimators=1000)
The accuracy for RFC = 0.5142666666666666
precision = 0.5191424196018377
recall = 0.3786651773247696
f1_score = 0.4379137736153722

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

5-KNeighborsClassifier¶

In [ ]:
knn_clf = KNeighborsClassifier()
param_grid = [{
    'n_neighbors':[3,4,5],
    'weights':['uniform','distance']
}]
grid_search = GridSearchCV(knn_clf, param_grid, cv=3,scoring='accuracy')
grid_search.fit(train,l1)
c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:824: UserWarning:

Scoring failed. The score on this train-test partition for these parameters will be set to nan. Details: 
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 813, in _score
    scores = scorer(estimator, X_test, y_test)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 266, in __call__
    return self._score(partial(_cached_call, None), estimator, X, y_true, **_kwargs)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 353, in _score
    y_pred = method_caller(estimator, "predict", X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_scorer.py", line 86, in _cached_call
    result, _ = _get_response_values(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_response.py", line 85, in _get_response_values
    y_pred = prediction_method(X)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\neighbors\_classification.py", line 246, in predict
    if self._fit_method == "brute" and ArgKminClassMode.is_usable_for(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 471, in is_usable_for
    ArgKmin.is_usable_for(X, Y, metric)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 115, in is_usable_for
    and (is_numpy_c_ordered(X) or is_valid_sparse_matrix(X))
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\metrics\_pairwise_distances_reduction\_dispatcher.py", line 99, in is_numpy_c_ordered
    return hasattr(X, "flags") and X.flags.c_contiguous
AttributeError: 'Flags' object has no attribute 'c_contiguous'


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_search.py:976: UserWarning:

One or more of the test scores are non-finite: [       nan 0.55399306        nan 0.55614583        nan 0.55208333]

Out[ ]:
GridSearchCV(cv=3, estimator=KNeighborsClassifier(),
             param_grid=[{'n_neighbors': [3, 4, 5],
                          'weights': ['uniform', 'distance']}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=KNeighborsClassifier(),
             param_grid=[{'n_neighbors': [3, 4, 5],
                          'weights': ['uniform', 'distance']}],
             scoring='accuracy')
KNeighborsClassifier()
KNeighborsClassifier()

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for KNeighborsClassifier = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'n_neighbors': 4, 'weights': 'distance'}
Best estimator = KNeighborsClassifier(n_neighbors=4, weights='distance')
The accuracy for KNeighborsClassifier = 0.5561458333333333
precision = 0.5614475998994722
recall = 0.623848087126501
f1_score = 0.591005291005291

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

6-MLPClassifier¶

In [ ]:
mlp_clf = MLPClassifier(max_iter = 2000)
param_grid = [{
    'activation':['identity', 'logistic'],
    'alpha':[0.0001,0.001],
    'learning_rate' :['constant','invscaling']
}]
grid_search = GridSearchCV(mlp_clf, param_grid, cv=3,scoring='accuracy')
grid_search.fit(train,l1)
Out[ ]:
GridSearchCV(cv=3, estimator=MLPClassifier(max_iter=2000),
             param_grid=[{'activation': ['identity', 'logistic'],
                          'alpha': [0.0001, 0.001],
                          'learning_rate': ['constant', 'invscaling']}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=MLPClassifier(max_iter=2000),
             param_grid=[{'activation': ['identity', 'logistic'],
                          'alpha': [0.0001, 0.001],
                          'learning_rate': ['constant', 'invscaling']}],
             scoring='accuracy')
MLPClassifier(max_iter=2000)
MLPClassifier(max_iter=2000)

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for MLPClassifier = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'activation': 'logistic', 'alpha': 0.0001, 'learning_rate': 'invscaling'}
Best estimator = MLPClassifier(activation='logistic', learning_rate='invscaling', max_iter=2000)
The accuracy for MLPClassifier = 0.5057986111111111
precision = 0.5069637883008357
recall = 0.6607092990784696
f1_score = 0.5737148399612026

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

7-CalibratedClassifierCV¶

In [ ]:
from sklearn.calibration import CalibratedClassifierCV
CC = CalibratedClassifierCV()
param_grid = [{
    'method' :['sigmoid', 'isotonic'],
    'estimator'  : ['estimator instance', None],
    'n_jobs': [-1,10,16]
}]
grid_search = GridSearchCV(CC, param_grid, cv=3,scoring='accuracy')
grid_search.fit(train,l1)
c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py:425: FitFailedWarning:


18 fits failed out of a total of 36.
The score on these train-test partitions for these parameters will be set to nan.
If these failures are not expected, you can try to debug them by setting error_score='raise'.

Below are more details about the failures:
--------------------------------------------------------------------------------
18 fits failed with the following error:
Traceback (most recent call last):
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_validation.py", line 732, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\base.py", line 1144, in wrapper
    estimator._validate_params()
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\base.py", line 637, in _validate_params
    validate_parameter_constraints(
  File "c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\utils\_param_validation.py", line 95, in validate_parameter_constraints
    raise InvalidParameterError(
sklearn.utils._param_validation.InvalidParameterError: The 'estimator' parameter of CalibratedClassifierCV must be an object implementing 'fit' and 'predict_proba', an object implementing 'fit' and 'decision_function' or None. Got 'estimator instance' instead.


c:\Users\DELL-G5\AppData\Local\Programs\Python\Python310\lib\site-packages\sklearn\model_selection\_search.py:976: UserWarning:

One or more of the test scores are non-finite: [       nan        nan        nan        nan        nan        nan
 0.49701389 0.49701389 0.49701389 0.50447917 0.50447917 0.50447917]

Out[ ]:
GridSearchCV(cv=3, estimator=CalibratedClassifierCV(),
             param_grid=[{'estimator': ['estimator instance', None],
                          'method': ['sigmoid', 'isotonic'],
                          'n_jobs': [-1, 10, 16]}],
             scoring='accuracy')
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
GridSearchCV(cv=3, estimator=CalibratedClassifierCV(),
             param_grid=[{'estimator': ['estimator instance', None],
                          'method': ['sigmoid', 'isotonic'],
                          'n_jobs': [-1, 10, 16]}],
             scoring='accuracy')
CalibratedClassifierCV()
CalibratedClassifierCV()

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=grid_search.predict(train)
P2=grid_search.predict(test)
print("Best parameter = "+str(grid_search.best_params_))
print("Best estimator = "+str(grid_search.best_estimator_))
print('The accuracy for CalibratedClassifierCV  = '+str(grid_search.best_score_))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
Best parameter = {'estimator': None, 'method': 'isotonic', 'n_jobs': -1}
Best estimator = CalibratedClassifierCV(method='isotonic', n_jobs=-1)
The accuracy for CalibratedClassifierCV  = 0.5044791666666667
precision = 0.5051597051597052
recall = 0.5741413013124825
f1_score = 0.5374460854790223

The confusion matrix for:

1-training set
2-testing set
In [ ]:
ConfusionMatrixDisplay.from_predictions(l1,P1)
ConfusionMatrixDisplay.from_predictions(l2,P2)
plt.show()
No description has been provided for this image
No description has been provided for this image

SGDClassifier used to find the tradeoff between the precision and the recall¶

In [ ]:
from sklearn.linear_model import SGDClassifier

sgd_clf = SGDClassifier(random_state=42)
sgd_clf.fit(train, l1)
Out[ ]:
SGDClassifier(random_state=42)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
SGDClassifier(random_state=42)

The accuracy, precision, recall, and f1 scores, for the test set :

In [ ]:
P1=cross_val_predict(sgd_clf, train, l1, cv=3)
P2=cross_val_predict(sgd_clf, test, l2, cv=3)

print('The accuracy for SGDClassifier  = '+str(accuracy_score(l2,P2)))
print("precision = "+str(precision_score(l2,P2)))
print("recall = "+str(recall_score(l2,P2)))
print("f1_score = "+str(f1_score(l2,P2)))
The accuracy for SGDClassifier  = 0.4990277777777778
precision = 0.49711367673179396
recall = 0.6252443451549846
f1_score = 0.553865182436611
In [ ]:
l2_scores = cross_val_predict(sgd_clf, test, l2, cv=3,
                             method="decision_function")
precisions, recalls, thresholds = precision_recall_curve(l2, l2_scores)
print("Precisions : ",precisions,"\n\n","Recalls : ", recalls,"\n\n", "Thresholds : ",thresholds)
Precisions :  [0.49736111 0.4974302  0.49749931 ... 0.5        0.         1.        ] 

 Recalls :  [1.00000000e+00 1.00000000e+00 1.00000000e+00 ... 2.79251606e-04
 0.00000000e+00 0.00000000e+00] 

 Thresholds :  [-3.19028312 -2.97208326 -2.8647555  ...  3.23812622  3.29804328
  3.30090686]
In [ ]:
threshold=0.7
plt.figure(figsize=(8, 4))
plt.plot(thresholds, precisions[:-1], "k--", label="Precision", linewidth=2)
plt.plot(thresholds, recalls[:-1], "c-", label="Recall", linewidth=2)
plt.vlines(threshold, 0, 1.0, "k", "dotted", label="threshold")
idx = (thresholds >= threshold).argmax()
plt.plot(thresholds[idx], precisions[idx], "ko")
plt.plot(thresholds[idx], recalls[idx], "co")
plt.axis([-4,4, 0, 1.1])
plt.grid()
plt.xlabel("Threshold")
plt.legend(loc="upper right")

plt.show()
No description has been provided for this image

PART 3¶

Neural networks

Three models are created in this part, two of which are sequential, and one is functional, as shown in the table below :

Properties seqModel1 seqModel2 funcModel
Activation function RelU elU RelU
Optimizer Adam RMSProp SGD
Initializer he_uniform he_uniform he_uniform
Dropout Yes (0.15) Yes (0.15) No

Here we split the training set to get validation set which represents 20% of the training set¶

In [ ]:
x_train= train[:23040]
y_train = l1[:23040]
x_valid= train[23040:]
y_valid = l1[23040:]
x_train.shape
Out[ ]:
(23040, 16)

seqModel1 :¶

In [ ]:
tf.random.set_seed(42)
model = tf.keras.Sequential()
model.add(layers.InputLayer(input_shape=(16)))

model.add(layers.Dense(400,kernel_initializer="he_normal",activation="relu"))
model.add(layers.Dropout(0.15))
model.add(layers.BatchNormalization())

model.add(layers.Dense(200,kernel_initializer="he_normal",activation="relu"))
model.add(layers.Dropout(0.15))
model.add(layers.BatchNormalization())

model.add(layers.Dense(100,kernel_initializer="he_normal",activation="relu"))
model.add(layers.Dropout(0.15))
model.add(layers.BatchNormalization())

model.add(layers.Dense(50,kernel_initializer="he_normal",activation="relu"))
model.add(layers.Dropout(0.15))
model.add(layers.BatchNormalization())

model.add(layers.Dense(25,kernel_initializer="he_normal",activation="relu"))
model.add(layers.Dropout(0.15))
model.add(layers.BatchNormalization())

model.add(layers.Dense(2,kernel_initializer="he_normal",activation="softmax"))
In [ ]:
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 400)               6800      
                                                                 
 dropout (Dropout)           (None, 400)               0         
                                                                 
 batch_normalization (Batch  (None, 400)               1600      
 Normalization)                                                  
                                                                 
 dense_1 (Dense)             (None, 200)               80200     
                                                                 
 dropout_1 (Dropout)         (None, 200)               0         
                                                                 
 batch_normalization_1 (Bat  (None, 200)               800       
 chNormalization)                                                
                                                                 
 dense_2 (Dense)             (None, 100)               20100     
                                                                 
 dropout_2 (Dropout)         (None, 100)               0         
                                                                 
 batch_normalization_2 (Bat  (None, 100)               400       
 chNormalization)                                                
                                                                 
 dense_3 (Dense)             (None, 50)                5050      
                                                                 
 dropout_3 (Dropout)         (None, 50)                0         
                                                                 
 batch_normalization_3 (Bat  (None, 50)                200       
 chNormalization)                                                
                                                                 
 dense_4 (Dense)             (None, 25)                1275      
                                                                 
 dropout_4 (Dropout)         (None, 25)                0         
                                                                 
 batch_normalization_4 (Bat  (None, 25)                100       
 chNormalization)                                                
                                                                 
 dense_5 (Dense)             (None, 2)                 52        
                                                                 
=================================================================
Total params: 116577 (455.38 KB)
Trainable params: 115027 (449.32 KB)
Non-trainable params: 1550 (6.05 KB)
_________________________________________________________________
In [ ]:
tf.keras.utils.plot_model(model, "medstu_model0.png", show_shapes=True)
Out[ ]:
No description has been provided for this image
In [ ]:
optimizer = keras.optimizers.Adam(learning_rate=1e-3)
model.compile(loss="sparse_categorical_crossentropy",
              optimizer=optimizer , metrics=["accuracy"])
In [ ]:
early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor="val_loss",patience=20)
out = model.fit(x_train, y_train, epochs=60, validation_data=(x_valid, y_valid),callbacks=[early_stopping_cb])
Epoch 1/60
720/720 [==============================] - 4s 3ms/step - loss: 0.7910 - accuracy: 0.4990 - val_loss: 0.6982 - val_accuracy: 0.4979
Epoch 2/60
720/720 [==============================] - 2s 3ms/step - loss: 0.7065 - accuracy: 0.5023 - val_loss: 0.6943 - val_accuracy: 0.5023
Epoch 3/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6967 - accuracy: 0.5087 - val_loss: 0.6946 - val_accuracy: 0.4957
Epoch 4/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6952 - accuracy: 0.5030 - val_loss: 0.6931 - val_accuracy: 0.5097
Epoch 5/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6942 - accuracy: 0.5044 - val_loss: 0.6934 - val_accuracy: 0.5016
Epoch 6/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6934 - accuracy: 0.5126 - val_loss: 0.6935 - val_accuracy: 0.5104
Epoch 7/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6938 - accuracy: 0.5072 - val_loss: 0.6936 - val_accuracy: 0.5007
Epoch 8/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6934 - accuracy: 0.5075 - val_loss: 0.6936 - val_accuracy: 0.4920
Epoch 9/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6931 - accuracy: 0.5151 - val_loss: 0.6935 - val_accuracy: 0.5021
Epoch 10/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6930 - accuracy: 0.5122 - val_loss: 0.6938 - val_accuracy: 0.5002
Epoch 11/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6929 - accuracy: 0.5157 - val_loss: 0.6938 - val_accuracy: 0.5042
Epoch 12/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6925 - accuracy: 0.5171 - val_loss: 0.6943 - val_accuracy: 0.5036
Epoch 13/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6924 - accuracy: 0.5139 - val_loss: 0.6944 - val_accuracy: 0.5047
Epoch 14/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6919 - accuracy: 0.5250 - val_loss: 0.6959 - val_accuracy: 0.5059
Epoch 15/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6918 - accuracy: 0.5229 - val_loss: 0.6945 - val_accuracy: 0.5026
Epoch 16/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6916 - accuracy: 0.5262 - val_loss: 0.6953 - val_accuracy: 0.5056
Epoch 17/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6905 - accuracy: 0.5289 - val_loss: 0.6957 - val_accuracy: 0.5071
Epoch 18/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6906 - accuracy: 0.5299 - val_loss: 0.6946 - val_accuracy: 0.5106
Epoch 19/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6891 - accuracy: 0.5349 - val_loss: 0.6979 - val_accuracy: 0.4967
Epoch 20/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6891 - accuracy: 0.5386 - val_loss: 0.6974 - val_accuracy: 0.5068
Epoch 21/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6872 - accuracy: 0.5457 - val_loss: 0.6955 - val_accuracy: 0.5078
Epoch 22/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6864 - accuracy: 0.5486 - val_loss: 0.6984 - val_accuracy: 0.5106
Epoch 23/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6860 - accuracy: 0.5518 - val_loss: 0.6963 - val_accuracy: 0.5052
Epoch 24/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6847 - accuracy: 0.5540 - val_loss: 0.6977 - val_accuracy: 0.5135
In [ ]:
out.params
Out[ ]:
{'verbose': 1, 'epochs': 60, 'steps': 720}
In [ ]:
t_loss,t_accuracy=model.evaluate(test, l2)
  1/225 [..............................] - ETA: 4s - loss: 0.7606 - accuracy: 0.2812225/225 [==============================] - 0s 1ms/step - loss: 0.6960 - accuracy: 0.5113
In [ ]:
model.evaluate(test,l2)
X = test[:20]
y_proba = model.predict(X)
y_proba.round(2)
y_pred = y_proba.argmax(axis=-1)
print("predictions: "+str(y_pred))
x=l2[:20].tolist()
print("labels: "+str(x))
225/225 [==============================] - 0s 1ms/step - loss: 0.6960 - accuracy: 0.5113
1/1 [==============================] - 0s 143ms/step
predictions: [1 0 0 1 0 0 1 1 1 0 1 1 0 0 0 0 1 1 1 1]
labels: [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0]
In [ ]:
#  "Accuracy"
plt.plot(out.history['accuracy'])
plt.plot(out.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(out.history['loss'])
plt.plot(out.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
No description has been provided for this image
No description has been provided for this image
In [ ]:
list1=["train_data",'test_data','valid_data']
list2=[max(out.history['accuracy']),t_accuracy,max(out.history['val_accuracy'])]
plt.bar(list1,list2,color=['plum','lavender','pink'])
plt.title('accuracy for validation, training and testing data')
plt.ylabel('accuracy')
Out[ ]:
Text(0, 0.5, 'accuracy')
No description has been provided for this image

seqModel2 :¶

In [ ]:
tf.random.set_seed(42)
model = tf.keras.Sequential()
model.add(layers.InputLayer(input_shape=(16)))

model.add(layers.Dense(400,kernel_initializer="he_normal"))
model.add(layers.BatchNormalization())
model.add(layers.Activation("elu"))
model.add(layers.Dropout(0.15))

model.add(layers.Dense(200,kernel_initializer="he_normal"))
model.add(layers.BatchNormalization())
model.add(layers.Activation("elu"))
model.add(layers.Dropout(0.15))

model.add(layers.Dense(100,kernel_initializer="he_normal"))
model.add(layers.BatchNormalization())
model.add(layers.Activation("elu"))
model.add(layers.Dropout(0.15))

model.add(layers.Dense(50,kernel_initializer="he_normal"))
model.add(layers.BatchNormalization())
model.add(layers.Activation("elu"))
model.add(layers.Dropout(0.15))

model.add(layers.Dense(25,kernel_initializer="he_normal"))
model.add(layers.BatchNormalization())
model.add(layers.Activation("elu"))
model.add(layers.Dropout(0.15))

model.add(layers.Dense(2,kernel_initializer="he_normal",activation="softmax"))
In [ ]:
model.summary()
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_6 (Dense)             (None, 400)               6800      
                                                                 
 batch_normalization_5 (Bat  (None, 400)               1600      
 chNormalization)                                                
                                                                 
 activation (Activation)     (None, 400)               0         
                                                                 
 dropout_5 (Dropout)         (None, 400)               0         
                                                                 
 dense_7 (Dense)             (None, 200)               80200     
                                                                 
 batch_normalization_6 (Bat  (None, 200)               800       
 chNormalization)                                                
                                                                 
 activation_1 (Activation)   (None, 200)               0         
                                                                 
 dropout_6 (Dropout)         (None, 200)               0         
                                                                 
 dense_8 (Dense)             (None, 100)               20100     
                                                                 
 batch_normalization_7 (Bat  (None, 100)               400       
 chNormalization)                                                
                                                                 
 activation_2 (Activation)   (None, 100)               0         
                                                                 
 dropout_7 (Dropout)         (None, 100)               0         
                                                                 
 dense_9 (Dense)             (None, 50)                5050      
                                                                 
 batch_normalization_8 (Bat  (None, 50)                200       
 chNormalization)                                                
                                                                 
 activation_3 (Activation)   (None, 50)                0         
                                                                 
 dropout_8 (Dropout)         (None, 50)                0         
                                                                 
 dense_10 (Dense)            (None, 25)                1275      
                                                                 
 batch_normalization_9 (Bat  (None, 25)                100       
 chNormalization)                                                
                                                                 
 activation_4 (Activation)   (None, 25)                0         
                                                                 
 dropout_9 (Dropout)         (None, 25)                0         
                                                                 
 dense_11 (Dense)            (None, 2)                 52        
                                                                 
=================================================================
Total params: 116577 (455.38 KB)
Trainable params: 115027 (449.32 KB)
Non-trainable params: 1550 (6.05 KB)
_________________________________________________________________
In [ ]:
optimizer = keras.optimizers.RMSprop(learning_rate=1e-3)
model.compile(loss="sparse_categorical_crossentropy",
              optimizer=optimizer,
              metrics=["accuracy"])
In [ ]:
tf.keras.utils.plot_model(model, "medstu_model1.png", show_shapes=True)
Out[ ]:
No description has been provided for this image
In [ ]:
early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor="val_loss",patience=20)

out = model.fit(x_train, y_train, epochs=60, validation_data=(x_valid, y_valid),callbacks=[early_stopping_cb])
Epoch 1/60
720/720 [==============================] - 4s 3ms/step - loss: 0.7313 - accuracy: 0.5009 - val_loss: 0.6973 - val_accuracy: 0.5012
Epoch 2/60
720/720 [==============================] - 2s 3ms/step - loss: 0.7024 - accuracy: 0.5070 - val_loss: 0.6940 - val_accuracy: 0.4995
Epoch 3/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6973 - accuracy: 0.5046 - val_loss: 0.6945 - val_accuracy: 0.5038
Epoch 4/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6953 - accuracy: 0.5002 - val_loss: 0.6934 - val_accuracy: 0.5057
Epoch 5/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6945 - accuracy: 0.4973 - val_loss: 0.6941 - val_accuracy: 0.4979
Epoch 6/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6938 - accuracy: 0.5062 - val_loss: 0.6933 - val_accuracy: 0.5019
Epoch 7/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6938 - accuracy: 0.5042 - val_loss: 0.6933 - val_accuracy: 0.5134
Epoch 8/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6937 - accuracy: 0.5050 - val_loss: 0.6929 - val_accuracy: 0.5113
Epoch 9/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6933 - accuracy: 0.5083 - val_loss: 0.6927 - val_accuracy: 0.5151
Epoch 10/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6936 - accuracy: 0.5086 - val_loss: 0.6936 - val_accuracy: 0.4908
Epoch 11/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6930 - accuracy: 0.5103 - val_loss: 0.6931 - val_accuracy: 0.5101
Epoch 12/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6932 - accuracy: 0.5106 - val_loss: 0.6936 - val_accuracy: 0.5075
Epoch 13/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6932 - accuracy: 0.5059 - val_loss: 0.6940 - val_accuracy: 0.5066
Epoch 14/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6932 - accuracy: 0.5094 - val_loss: 0.6934 - val_accuracy: 0.5125
Epoch 15/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6931 - accuracy: 0.5090 - val_loss: 0.6938 - val_accuracy: 0.5042
Epoch 16/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6930 - accuracy: 0.5115 - val_loss: 0.6939 - val_accuracy: 0.5038
Epoch 17/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6928 - accuracy: 0.5118 - val_loss: 0.6958 - val_accuracy: 0.5167
Epoch 18/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6931 - accuracy: 0.5070 - val_loss: 0.6947 - val_accuracy: 0.4997
Epoch 19/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6928 - accuracy: 0.5130 - val_loss: 0.6948 - val_accuracy: 0.5014
Epoch 20/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6930 - accuracy: 0.5094 - val_loss: 0.6937 - val_accuracy: 0.5089
Epoch 21/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6922 - accuracy: 0.5173 - val_loss: 0.6937 - val_accuracy: 0.5042
Epoch 22/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6926 - accuracy: 0.5159 - val_loss: 0.6948 - val_accuracy: 0.5071
Epoch 23/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6926 - accuracy: 0.5150 - val_loss: 0.6942 - val_accuracy: 0.5122
Epoch 24/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6921 - accuracy: 0.5185 - val_loss: 0.6937 - val_accuracy: 0.5104
Epoch 25/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6924 - accuracy: 0.5198 - val_loss: 0.6938 - val_accuracy: 0.5101
Epoch 26/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6921 - accuracy: 0.5222 - val_loss: 0.6936 - val_accuracy: 0.5130
Epoch 27/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6921 - accuracy: 0.5201 - val_loss: 0.6939 - val_accuracy: 0.5052
Epoch 28/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6918 - accuracy: 0.5192 - val_loss: 0.6954 - val_accuracy: 0.5101
Epoch 29/60
720/720 [==============================] - 2s 3ms/step - loss: 0.6923 - accuracy: 0.5165 - val_loss: 0.6946 - val_accuracy: 0.5019
In [ ]:
t_val,t_accuracy=model.evaluate(test, l2)
  1/225 [..............................] - ETA: 4s - loss: 0.7088 - accuracy: 0.3125225/225 [==============================] - 0s 1ms/step - loss: 0.6934 - accuracy: 0.5106
In [ ]:
model.evaluate(test,l2)
X = test[:20]
y_proba = model.predict(X)
y_proba.round(2)
y_pred = y_proba.argmax(axis=-1)
print("predictions: "+str(y_pred))
x=l2[:20].tolist()
print("labels: "+str(x))
225/225 [==============================] - 0s 1ms/step - loss: 0.6934 - accuracy: 0.5106
1/1 [==============================] - 0s 116ms/step
predictions: [0 0 0 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0]
labels: [0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0]
In [ ]:
print(out.history.keys())
#  "Accuracy"
plt.plot(out.history['accuracy'])
plt.plot(out.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.grid(True)
plt.show()
# "Loss"
plt.plot(out.history['loss'])
plt.plot(out.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.grid(True)
plt.show()
dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy'])
No description has been provided for this image
No description has been provided for this image
In [ ]:
list1=["train_data",'test_data','valid_data']
list2=[max(out.history['accuracy']),t_accuracy,max(out.history['val_accuracy'])]
plt.bar(list1,list2,color=['plum','lavender','pink'])
plt.title('accuracy for validation training and testing data')
plt.ylabel('accuracy')
Out[ ]:
Text(0, 0.5, 'accuracy')
No description has been provided for this image

funcModel :¶

In [ ]:
L1=layers.Input(shape=[6])
L2=layers.Input(shape=[12])
norm_1 = layers.BatchNormalization()(L1)
hidden1 =layers.Dense(50, activation="relu",kernel_initializer="he_uniform")(norm_1)
norm_2 = layers.BatchNormalization()(hidden1)
hidden2 =layers.Dense(50, activation="relu",kernel_initializer="he_uniform")(norm_2)
norm_3 = layers.BatchNormalization()(hidden2)
norm_4 = layers.BatchNormalization()(L2)
concat =layers.concatenate([norm_4, norm_3])
output1 = keras.layers.Dense(1 , name='Moutput')(concat)
output2 = keras.layers.Dense(1 , name='Aoutput')(norm_3)
model = keras.models.Model(inputs=[L1,L2], outputs=[output1,output2])
In [ ]:
model.summary()
Model: "model_1"
__________________________________________________________________________________________________
 Layer (type)                Output Shape                 Param #   Connected to                  
==================================================================================================
 input_5 (InputLayer)        [(None, 6)]                  0         []                            
                                                                                                  
 batch_normalization_13 (Ba  (None, 6)                    24        ['input_5[0][0]']             
 tchNormalization)                                                                                
                                                                                                  
 dense_14 (Dense)            (None, 50)                   350       ['batch_normalization_13[0][0]
                                                                    ']                            
                                                                                                  
 batch_normalization_14 (Ba  (None, 50)                   200       ['dense_14[0][0]']            
 tchNormalization)                                                                                
                                                                                                  
 input_6 (InputLayer)        [(None, 12)]                 0         []                            
                                                                                                  
 dense_15 (Dense)            (None, 50)                   2550      ['batch_normalization_14[0][0]
                                                                    ']                            
                                                                                                  
 batch_normalization_16 (Ba  (None, 12)                   48        ['input_6[0][0]']             
 tchNormalization)                                                                                
                                                                                                  
 batch_normalization_15 (Ba  (None, 50)                   200       ['dense_15[0][0]']            
 tchNormalization)                                                                                
                                                                                                  
 concatenate_1 (Concatenate  (None, 62)                   0         ['batch_normalization_16[0][0]
 )                                                                  ',                            
                                                                     'batch_normalization_15[0][0]
                                                                    ']                            
                                                                                                  
 Moutput (Dense)             (None, 1)                    63        ['concatenate_1[0][0]']       
                                                                                                  
 Aoutput (Dense)             (None, 1)                    51        ['batch_normalization_15[0][0]
                                                                    ']                            
                                                                                                  
==================================================================================================
Total params: 3486 (13.62 KB)
Trainable params: 3250 (12.70 KB)
Non-trainable params: 236 (944.00 Byte)
__________________________________________________________________________________________________
In [ ]:
tf.keras.utils.plot_model(model, "medstu_model2.png", show_shapes=True)
Out[ ]:
No description has been provided for this image
In [ ]:
model.compile(loss=["mean_squared_error","mean_squared_error"], optimizer=keras.optimizers.SGD(learning_rate=1e-3), metrics=["accuracy"])
In [ ]:
X_train_A, X_train_B = x_train.iloc[:, :6], x_train.iloc[:, 4:]
X_valid_A, X_valid_B = x_valid.iloc[:, :6], x_valid.iloc[:, 4:]
X_test_A, X_test_B = test.iloc[:, :6], test.iloc[:, 4:]
X_new_A, X_new_B = X_test_A.iloc[:6], X_test_B.iloc[:6]
In [ ]:
history = model.fit((X_train_A, X_train_B), y_train, epochs=40,validation_data=((X_valid_A, X_valid_B), y_valid))
Epoch 1/40
720/720 [==============================] - 3s 2ms/step - loss: 1.0952 - Moutput_loss: 0.5533 - Aoutput_loss: 0.5419 - Moutput_accuracy: 0.5000 - Aoutput_accuracy: 0.4953 - val_loss: 0.6960 - val_Moutput_loss: 0.3438 - val_Aoutput_loss: 0.3522 - val_Moutput_accuracy: 0.4984 - val_Aoutput_accuracy: 0.4958
Epoch 2/40
720/720 [==============================] - 1s 2ms/step - loss: 0.6814 - Moutput_loss: 0.3334 - Aoutput_loss: 0.3479 - Moutput_accuracy: 0.5007 - Aoutput_accuracy: 0.4935 - val_loss: 0.6072 - val_Moutput_loss: 0.3008 - val_Aoutput_loss: 0.3064 - val_Moutput_accuracy: 0.4988 - val_Aoutput_accuracy: 0.5054
Epoch 3/40
720/720 [==============================] - 1s 2ms/step - loss: 0.6176 - Moutput_loss: 0.3041 - Aoutput_loss: 0.3135 - Moutput_accuracy: 0.5004 - Aoutput_accuracy: 0.4976 - val_loss: 0.5741 - val_Moutput_loss: 0.2851 - val_Aoutput_loss: 0.2890 - val_Moutput_accuracy: 0.4967 - val_Aoutput_accuracy: 0.5068
Epoch 4/40
720/720 [==============================] - 2s 2ms/step - loss: 0.5857 - Moutput_loss: 0.2896 - Aoutput_loss: 0.2961 - Moutput_accuracy: 0.5033 - Aoutput_accuracy: 0.5030 - val_loss: 0.5540 - val_Moutput_loss: 0.2756 - val_Aoutput_loss: 0.2785 - val_Moutput_accuracy: 0.5049 - val_Aoutput_accuracy: 0.5056
Epoch 5/40
720/720 [==============================] - 2s 2ms/step - loss: 0.5667 - Moutput_loss: 0.2803 - Aoutput_loss: 0.2864 - Moutput_accuracy: 0.5034 - Aoutput_accuracy: 0.4994 - val_loss: 0.5408 - val_Moutput_loss: 0.2693 - val_Aoutput_loss: 0.2715 - val_Moutput_accuracy: 0.5005 - val_Aoutput_accuracy: 0.5082
Epoch 6/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5538 - Moutput_loss: 0.2750 - Aoutput_loss: 0.2788 - Moutput_accuracy: 0.5065 - Aoutput_accuracy: 0.5007 - val_loss: 0.5333 - val_Moutput_loss: 0.2657 - val_Aoutput_loss: 0.2677 - val_Moutput_accuracy: 0.5033 - val_Aoutput_accuracy: 0.5063
Epoch 7/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5465 - Moutput_loss: 0.2717 - Aoutput_loss: 0.2748 - Moutput_accuracy: 0.5005 - Aoutput_accuracy: 0.5008 - val_loss: 0.5270 - val_Moutput_loss: 0.2628 - val_Aoutput_loss: 0.2642 - val_Moutput_accuracy: 0.5031 - val_Aoutput_accuracy: 0.5080
Epoch 8/40
720/720 [==============================] - 2s 3ms/step - loss: 0.5367 - Moutput_loss: 0.2666 - Aoutput_loss: 0.2701 - Moutput_accuracy: 0.5069 - Aoutput_accuracy: 0.5022 - val_loss: 0.5231 - val_Moutput_loss: 0.2608 - val_Aoutput_loss: 0.2623 - val_Moutput_accuracy: 0.5031 - val_Aoutput_accuracy: 0.5059
Epoch 9/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5314 - Moutput_loss: 0.2644 - Aoutput_loss: 0.2670 - Moutput_accuracy: 0.5058 - Aoutput_accuracy: 0.5022 - val_loss: 0.5187 - val_Moutput_loss: 0.2590 - val_Aoutput_loss: 0.2597 - val_Moutput_accuracy: 0.5049 - val_Aoutput_accuracy: 0.5049
Epoch 10/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5294 - Moutput_loss: 0.2643 - Aoutput_loss: 0.2652 - Moutput_accuracy: 0.4980 - Aoutput_accuracy: 0.4997 - val_loss: 0.5167 - val_Moutput_loss: 0.2579 - val_Aoutput_loss: 0.2588 - val_Moutput_accuracy: 0.5003 - val_Aoutput_accuracy: 0.5054
Epoch 11/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5232 - Moutput_loss: 0.2607 - Aoutput_loss: 0.2625 - Moutput_accuracy: 0.5040 - Aoutput_accuracy: 0.5039 - val_loss: 0.5141 - val_Moutput_loss: 0.2565 - val_Aoutput_loss: 0.2576 - val_Moutput_accuracy: 0.5059 - val_Aoutput_accuracy: 0.5099
Epoch 12/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5217 - Moutput_loss: 0.2599 - Aoutput_loss: 0.2618 - Moutput_accuracy: 0.5025 - Aoutput_accuracy: 0.5005 - val_loss: 0.5122 - val_Moutput_loss: 0.2559 - val_Aoutput_loss: 0.2563 - val_Moutput_accuracy: 0.5028 - val_Aoutput_accuracy: 0.5120
Epoch 13/40
720/720 [==============================] - 2s 3ms/step - loss: 0.5190 - Moutput_loss: 0.2590 - Aoutput_loss: 0.2599 - Moutput_accuracy: 0.5023 - Aoutput_accuracy: 0.5026 - val_loss: 0.5106 - val_Moutput_loss: 0.2550 - val_Aoutput_loss: 0.2556 - val_Moutput_accuracy: 0.5059 - val_Aoutput_accuracy: 0.5050
Epoch 14/40
720/720 [==============================] - 2s 2ms/step - loss: 0.5163 - Moutput_loss: 0.2577 - Aoutput_loss: 0.2586 - Moutput_accuracy: 0.5069 - Aoutput_accuracy: 0.5052 - val_loss: 0.5092 - val_Moutput_loss: 0.2543 - val_Aoutput_loss: 0.2549 - val_Moutput_accuracy: 0.5031 - val_Aoutput_accuracy: 0.5082
Epoch 15/40
720/720 [==============================] - 2s 2ms/step - loss: 0.5146 - Moutput_loss: 0.2566 - Aoutput_loss: 0.2580 - Moutput_accuracy: 0.5004 - Aoutput_accuracy: 0.5026 - val_loss: 0.5085 - val_Moutput_loss: 0.2540 - val_Aoutput_loss: 0.2545 - val_Moutput_accuracy: 0.5050 - val_Aoutput_accuracy: 0.5080
Epoch 16/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5130 - Moutput_loss: 0.2558 - Aoutput_loss: 0.2571 - Moutput_accuracy: 0.5069 - Aoutput_accuracy: 0.5051 - val_loss: 0.5081 - val_Moutput_loss: 0.2538 - val_Aoutput_loss: 0.2543 - val_Moutput_accuracy: 0.4972 - val_Aoutput_accuracy: 0.5028
Epoch 17/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5114 - Moutput_loss: 0.2553 - Aoutput_loss: 0.2561 - Moutput_accuracy: 0.5062 - Aoutput_accuracy: 0.5086 - val_loss: 0.5078 - val_Moutput_loss: 0.2539 - val_Aoutput_loss: 0.2539 - val_Moutput_accuracy: 0.5047 - val_Aoutput_accuracy: 0.5026
Epoch 18/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5106 - Moutput_loss: 0.2550 - Aoutput_loss: 0.2556 - Moutput_accuracy: 0.5039 - Aoutput_accuracy: 0.5059 - val_loss: 0.5058 - val_Moutput_loss: 0.2527 - val_Aoutput_loss: 0.2532 - val_Moutput_accuracy: 0.5092 - val_Aoutput_accuracy: 0.5010
Epoch 19/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5094 - Moutput_loss: 0.2544 - Aoutput_loss: 0.2551 - Moutput_accuracy: 0.5054 - Aoutput_accuracy: 0.5075 - val_loss: 0.5072 - val_Moutput_loss: 0.2535 - val_Aoutput_loss: 0.2536 - val_Moutput_accuracy: 0.5007 - val_Aoutput_accuracy: 0.4998
Epoch 20/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5090 - Moutput_loss: 0.2545 - Aoutput_loss: 0.2545 - Moutput_accuracy: 0.5051 - Aoutput_accuracy: 0.5073 - val_loss: 0.5051 - val_Moutput_loss: 0.2524 - val_Aoutput_loss: 0.2527 - val_Moutput_accuracy: 0.5099 - val_Aoutput_accuracy: 0.5061
Epoch 21/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5086 - Moutput_loss: 0.2539 - Aoutput_loss: 0.2547 - Moutput_accuracy: 0.5102 - Aoutput_accuracy: 0.5064 - val_loss: 0.5045 - val_Moutput_loss: 0.2521 - val_Aoutput_loss: 0.2524 - val_Moutput_accuracy: 0.5052 - val_Aoutput_accuracy: 0.5054
Epoch 22/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5074 - Moutput_loss: 0.2534 - Aoutput_loss: 0.2540 - Moutput_accuracy: 0.5065 - Aoutput_accuracy: 0.5030 - val_loss: 0.5040 - val_Moutput_loss: 0.2518 - val_Aoutput_loss: 0.2522 - val_Moutput_accuracy: 0.5010 - val_Aoutput_accuracy: 0.5073
Epoch 23/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5063 - Moutput_loss: 0.2529 - Aoutput_loss: 0.2534 - Moutput_accuracy: 0.5065 - Aoutput_accuracy: 0.5069 - val_loss: 0.5036 - val_Moutput_loss: 0.2516 - val_Aoutput_loss: 0.2520 - val_Moutput_accuracy: 0.4983 - val_Aoutput_accuracy: 0.5045
Epoch 24/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5062 - Moutput_loss: 0.2529 - Aoutput_loss: 0.2533 - Moutput_accuracy: 0.5069 - Aoutput_accuracy: 0.5042 - val_loss: 0.5037 - val_Moutput_loss: 0.2517 - val_Aoutput_loss: 0.2520 - val_Moutput_accuracy: 0.5014 - val_Aoutput_accuracy: 0.5023
Epoch 25/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5050 - Moutput_loss: 0.2521 - Aoutput_loss: 0.2528 - Moutput_accuracy: 0.5102 - Aoutput_accuracy: 0.5075 - val_loss: 0.5029 - val_Moutput_loss: 0.2513 - val_Aoutput_loss: 0.2516 - val_Moutput_accuracy: 0.5089 - val_Aoutput_accuracy: 0.5097
Epoch 26/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5051 - Moutput_loss: 0.2526 - Aoutput_loss: 0.2525 - Moutput_accuracy: 0.5040 - Aoutput_accuracy: 0.5075 - val_loss: 0.5027 - val_Moutput_loss: 0.2511 - val_Aoutput_loss: 0.2516 - val_Moutput_accuracy: 0.5033 - val_Aoutput_accuracy: 0.5061
Epoch 27/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5054 - Moutput_loss: 0.2526 - Aoutput_loss: 0.2528 - Moutput_accuracy: 0.5056 - Aoutput_accuracy: 0.5077 - val_loss: 0.5025 - val_Moutput_loss: 0.2511 - val_Aoutput_loss: 0.2514 - val_Moutput_accuracy: 0.5097 - val_Aoutput_accuracy: 0.4965
Epoch 28/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5043 - Moutput_loss: 0.2519 - Aoutput_loss: 0.2524 - Moutput_accuracy: 0.5071 - Aoutput_accuracy: 0.5053 - val_loss: 0.5023 - val_Moutput_loss: 0.2510 - val_Aoutput_loss: 0.2513 - val_Moutput_accuracy: 0.5115 - val_Aoutput_accuracy: 0.4988
Epoch 29/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5035 - Moutput_loss: 0.2516 - Aoutput_loss: 0.2519 - Moutput_accuracy: 0.5045 - Aoutput_accuracy: 0.5120 - val_loss: 0.5018 - val_Moutput_loss: 0.2507 - val_Aoutput_loss: 0.2511 - val_Moutput_accuracy: 0.5054 - val_Aoutput_accuracy: 0.5047
Epoch 30/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5035 - Moutput_loss: 0.2517 - Aoutput_loss: 0.2518 - Moutput_accuracy: 0.5055 - Aoutput_accuracy: 0.5052 - val_loss: 0.5021 - val_Moutput_loss: 0.2509 - val_Aoutput_loss: 0.2512 - val_Moutput_accuracy: 0.5075 - val_Aoutput_accuracy: 0.5095
Epoch 31/40
720/720 [==============================] - 1s 1ms/step - loss: 0.5033 - Moutput_loss: 0.2515 - Aoutput_loss: 0.2517 - Moutput_accuracy: 0.5038 - Aoutput_accuracy: 0.5090 - val_loss: 0.5023 - val_Moutput_loss: 0.2510 - val_Aoutput_loss: 0.2512 - val_Moutput_accuracy: 0.5061 - val_Aoutput_accuracy: 0.5043
Epoch 32/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5032 - Moutput_loss: 0.2514 - Aoutput_loss: 0.2518 - Moutput_accuracy: 0.5082 - Aoutput_accuracy: 0.5088 - val_loss: 0.5013 - val_Moutput_loss: 0.2505 - val_Aoutput_loss: 0.2508 - val_Moutput_accuracy: 0.5113 - val_Aoutput_accuracy: 0.5038
Epoch 33/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5022 - Moutput_loss: 0.2511 - Aoutput_loss: 0.2512 - Moutput_accuracy: 0.5104 - Aoutput_accuracy: 0.5117 - val_loss: 0.5020 - val_Moutput_loss: 0.2509 - val_Aoutput_loss: 0.2511 - val_Moutput_accuracy: 0.5076 - val_Aoutput_accuracy: 0.5003
Epoch 34/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5019 - Moutput_loss: 0.2508 - Aoutput_loss: 0.2511 - Moutput_accuracy: 0.5140 - Aoutput_accuracy: 0.5128 - val_loss: 0.5021 - val_Moutput_loss: 0.2510 - val_Aoutput_loss: 0.2511 - val_Moutput_accuracy: 0.5036 - val_Aoutput_accuracy: 0.5009
Epoch 35/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5021 - Moutput_loss: 0.2509 - Aoutput_loss: 0.2512 - Moutput_accuracy: 0.5093 - Aoutput_accuracy: 0.5094 - val_loss: 0.5016 - val_Moutput_loss: 0.2508 - val_Aoutput_loss: 0.2508 - val_Moutput_accuracy: 0.5066 - val_Aoutput_accuracy: 0.5047
Epoch 36/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5022 - Moutput_loss: 0.2510 - Aoutput_loss: 0.2512 - Moutput_accuracy: 0.5081 - Aoutput_accuracy: 0.5122 - val_loss: 0.5022 - val_Moutput_loss: 0.2511 - val_Aoutput_loss: 0.2512 - val_Moutput_accuracy: 0.5085 - val_Aoutput_accuracy: 0.5036
Epoch 37/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5025 - Moutput_loss: 0.2511 - Aoutput_loss: 0.2513 - Moutput_accuracy: 0.5109 - Aoutput_accuracy: 0.5079 - val_loss: 0.5021 - val_Moutput_loss: 0.2510 - val_Aoutput_loss: 0.2511 - val_Moutput_accuracy: 0.5064 - val_Aoutput_accuracy: 0.5031
Epoch 38/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5023 - Moutput_loss: 0.2510 - Aoutput_loss: 0.2513 - Moutput_accuracy: 0.5091 - Aoutput_accuracy: 0.5081 - val_loss: 0.5014 - val_Moutput_loss: 0.2507 - val_Aoutput_loss: 0.2507 - val_Moutput_accuracy: 0.5056 - val_Aoutput_accuracy: 0.5078
Epoch 39/40
720/720 [==============================] - 2s 2ms/step - loss: 0.5018 - Moutput_loss: 0.2508 - Aoutput_loss: 0.2510 - Moutput_accuracy: 0.5116 - Aoutput_accuracy: 0.5102 - val_loss: 0.5014 - val_Moutput_loss: 0.2507 - val_Aoutput_loss: 0.2507 - val_Moutput_accuracy: 0.5085 - val_Aoutput_accuracy: 0.5068
Epoch 40/40
720/720 [==============================] - 1s 2ms/step - loss: 0.5014 - Moutput_loss: 0.2508 - Aoutput_loss: 0.2506 - Moutput_accuracy: 0.5068 - Aoutput_accuracy: 0.5128 - val_loss: 0.5011 - val_Moutput_loss: 0.2505 - val_Aoutput_loss: 0.2506 - val_Moutput_accuracy: 0.5054 - val_Aoutput_accuracy: 0.5005
In [ ]:
mse_test = model.evaluate((X_test_A, X_test_B), l2)
y_pred = model.predict((X_new_A, X_new_B))
225/225 [==============================] - 0s 970us/step - loss: 0.5019 - Moutput_loss: 0.2510 - Aoutput_loss: 0.2509 - Moutput_accuracy: 0.5049 - Aoutput_accuracy: 0.5058
1/1 [==============================] - 0s 103ms/step
In [ ]:
pd.DataFrame(history.history).plot(
    figsize=(10, 7), ylim=[0,1], grid=True, xlabel="Epoch",
    style=["r--", "r--.", "b-", "b-*"])
plt.legend(loc="upper right")
plt.show()
No description has been provided for this image
In [ ]: